<template>
	<view class="top_view">
		<view class="list" @click="clear">
			<view class="left">
				<image src="@/static/images/ccIco.png" mode=""></image>
				清除缓存
			</view>
			<!-- #ifdef APP-PLUS -->
			<view class="right">
				<text>{{fileSizeString}}</text>
			</view>
			<!-- #endif -->
		</view>
		<view style="display: none;" class="list">
			<image src="@/static/images/hyIco.png" mode=""></image><text>欢迎页</text>
		</view>
		<view class="list" @click="go('aboutUs')">
			<view class="left">
				<image src="@/static/images/gwIco.png" mode=""></image><text>关于我们</text>
			</view>
		</view>
		<view class="list" @click="go('serve')">
			<view class="left">
				<image src="@/static/images/serveLogo.png" mode=""></image><text>用户服务协议</text>
			</view>
		</view>
		<view class="list" @click="go('serve?type=1')">
			<view class="left">
				<image src="../static/images/ysxy.png" mode=""></image><text>隐私协议</text>
			</view>
		</view>
		<view class="list" @click="go('feedback')" >
			<view class="left">
				<image src="../static/images/yjfk.png" mode=""></image><text>意见反馈</text>
			</view>
		</view>
		<view class="list" @click="cancellation">
			<view class="left">
			<image src="/static/images/cancellation.png" mode=""></image>
				<text>账号注销</text>
			</view>
		</view>
		<view class="login_out" @click="lgoinOut()">
			退出登录
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileSizeString: '0B',
			}
		},
		methods: {
			//是否是微信内置浏览器
			isWeiXin() {
				// #ifdef H5
				let ua = window.navigator.userAgent.toLowerCase();
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					return true;
				} else {
					return false;
				}
				// #endif
			},
			clear() {
				uni.showModal({
					content: '确定要清除缓存吗？',
					cancelText: "取消", // 取消按钮的文字  
					confirmText: "确认", // 确认按钮的文字  
					showCancel: true, // 是否显示取消按钮，默认为 true
					confirmColor: '#f55850',
					cancelColor: '#39B54A',
					success: (res) => {
						if (res.confirm) {
							console.log('comfirm') //点击确定之后执行的代码
							// #ifdef APP-PLUS
							let that = this;
							let os = plus.os.name;
							if (os == 'Android') {
								let main = plus.android.runtimeMainActivity();
								let sdRoot = main.getCacheDir();
								let files = plus.android.invoke(sdRoot, "listFiles");
								let len = files.length;
								for (let i = 0; i < len; i++) {
									let filePath = '' + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径  
									plus.io.resolveLocalFileSystemURL(filePath, function(entry) {
										if (entry.isDirectory) {
											entry.removeRecursively(function(
											entry) { //递归删除其下的所有文件及子目录  
												uni.showToast({
													title: '缓存清理完成',
													duration: 2000
												});
												that.formatSize(); // 重新计算缓存  
											}, function(e) {
												console.log(e.message)
											});
										} else {
											entry.remove();
										}
									}, function(e) {
										console.log('文件路径读取失败')
									});
								}
							} else { // ios  
								plus.cache.clear(function() {
									uni.showToast({
										title: '缓存清理完成',
										duration: 2000
									});
									that.formatSize();
								});
							}
						    // #endif
						} else {
							console.log('cancel') //点击取消之后执行的代码
						}
					}
				})
			},
			// 计算缓存
			formatSize() {
				// #ifdef APP-PLUS
				let that = this;
				plus.cache.calculate(function(size) {
					let sizeCache = parseInt(size);
					if (sizeCache == 0) {
						that.fileSizeString = "0B";
					} else if (sizeCache < 1024) {
						that.fileSizeString = sizeCache + "B";
					} else if (sizeCache < 1048576) {
						that.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
					} else if (sizeCache < 1073741824) {
						that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
					} else {
						that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
					}
				});
				// #endif
			},
			cancellation() {
				let that = this
				uni.showModal({
					title: '提示',
					content: '是否确认注销此账号，注销后此账号将无法登录',
					success: function(res) {
						if (res.confirm) {
							that.$util.request({
								url: '/mobile/index.php?act=member_security&op=cancellation',
								method: 'get',
								data: {}
							}).then(res => {
								if (res.error_code == 0) {
									uni.showToast({
										title: '注销成功',
										icon: "none"
									})
									setTimeout(() => {
										that.lgoinOut()
									}, 1500)
								} else {
									uni.showToast({
										title: res.message,
										icon: "none"
									})
								}
							})
						} else if (res.cancel) {

						}
					}
				})
			},
			//退出登录
			lgoinOut() {
				uni.clearStorageSync();
				uni.reLaunch({
					url: '/main/home/index'
				})
			},
			//跳转
			go(url){
				uni.navigateTo({
					url:url
				})
			}
		}
	}
</script>

<style>
	.top_view {
		padding-top: 15rpx;
	}

	.login_out {
		height: 80rpx;
		width: 80%;
		margin: auto;
		background: linear-gradient(to right, #ff4300, #ff4300);
		color: #FFFFFF;
		text-align: center;
		line-height: 80rpx;
		font-size: 30rpx;
		position: fixed;
		bottom: 25%;
		left: 10%;
	}

	.list {
		margin: 15rpx 0;
		background-color: #FFFFFF;
		color: #000000;
		padding: 0 20rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.list image {
		width: 57rpx;
		height: 57rpx;
		margin: 15rpx;
		vertical-align: middle;
		margin-right: 15rpx;
		border-radius: 10rpx;
	}

	.list:first-child {
		margin-top: 0;
	}
</style>